﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using NGPT.Utils;

namespace NGPT.PostAnywhere.WebParts
{
    [ToolboxItemAttribute(false)]
    public class PostAnywhereWebPart : WebPart
    {
        private const bool AddPageURLDefault = true;

        public PostAnywhereWebPart()
        {
            AddPageURL = AddPageURLDefault;
        }

        /// <summary>
        /// Add Page URL parameter.
        /// </summary>
        [WebBrowsable(true), WebDisplayName("Add Page URL"), WebDescription("Check if you need to add current page URL to a microblog text by default."), Personalizable(PersonalizationScope.Shared), Category("Custom Properties"), DefaultValue(AddPageURLDefault)]
        public bool AddPageURL { get; set; }

        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();

                WebControl container = new WebControl(System.Web.UI.HtmlTextWriterTag.Div);
                container.ID = "BookmarkletLinkButton";
                container.CssClass = "bookmarkletLinkButton";
                container.Style.Value = "border: 1px solid #CCC; margin: 10px; padding: 8px 10px 8px 25px; max-width: 350px;";
                container.Controls.Add(new LiteralControl("Drag the following link to your browser’s toolbar to create bookmarklets.<br/>"));

                string bookmarkletHref = 
                    "javascript: (function () {" + 
                    "var a = \"" + SPContext.Current.Web.Site.WebApplication.Sites[0].Url + "\";" +
                    "var b;" +
                    "var c = {};" +
                    "c.PA = {};" +
                    "c.PA.Bookmarklet = {};" +
                    "c.PA.Bookmarklet.LoadJquery = function (c, d, e, f) {" +
                    "    var g, h, i;" +
                        "if (!(f = c.jQuery)) {" +
                            "g = d.createElement(\"script\");" +
                            "g.type = \"text/javascript\";" +
                            "g.src = a + \"/_layouts/jquery/jquery-1.10.2.min.js\";" +
                            "g.onload = g.onreadystatechange = function () {" +
                                "if (!h && (!(i = this.readyState) || i == \"loaded\" || i == \"complete\")) {" +
                                    "b = (f = c.jQuery).noConflict(1);" +
                                    "e();" +
                                    "f(g).remove()" +
                                "}" +
                            "};" +
                            "d.documentElement.childNodes[0].appendChild(g)" +
                        "}" +
                        "else {" +
                            "b = f;" +
                            "e()" +
                        "}" +
                    "};" +

                    "c.PA.Bookmarklet.GetSelected = function () {" +
                        "var t = '';" +
                        "if (window.getSelection) {" +
                            "t = window.getSelection();" +
                        "} else if (document.getSelection) {" +
                            "t = document.getSelection();" +
                        "} else if (document.selection) {" +
                            "t = document.selection.createRange().text;" +
                        "}" +
                        "return t;" +
                    "};" +

                    "c.PA.Bookmarklet.Show = function () {" +
                        "var r = window.location.href;" +
                        "var d = c.PA.Bookmarklet.GetSelected()" + ((AddPageURL) ? "+ ' \\n' + r;" : ";") +
                        "var e = a + \"/_layouts/ng/pages/postcontrol.aspx?isdlg=1&default=\" + encodeURIComponent(d);" +
                        "var f = b(\"<div id='ngExtPostControlContainer' class='ngExtStreamPopup' style='display: block;'><link rel=StyleSheet href='\" + a + \"/_layouts/ng/script/stream/stream.css' type='text/css' media=screen><div id='ngExtPostClose' class='closeLink'>x</div></div>\");" +
                        "var g = b(\"<iframe id='NgBookmarkletIframe' frameBorder='0' style='border:none; height:550px; width:620px;' seamless></iframe>\").attr(\"src\", e);" +
                        "f.append(g);" +
                        "b(\"body\").append(f);" +
                        "b(g).load(function () {" +
                            "b(\"#ngExtPostControlContainer .closeLink\").click(function () {" +
                                "b(f).slideUp(\"fast\", function () {" +
                                    "b(document).remove(f)" +
                                "})" +
                            "})" +
                        "})" +
                    "};" +
                    "c.PA.Bookmarklet.JqueryLoadedCallback = function () {" +
                        "c.PA.Bookmarklet.Show()" +
                    "};" +
                    "c.PA.Bookmarklet.Start = function () {" +
                        "c.PA.Bookmarklet.LoadJquery(window, document, function () {" +
                            "c.PA.Bookmarklet.JqueryLoadedCallback()" +
                        "})" +
                    "};" +
                    "c.PA.Bookmarklet.Start()" +
                "})();";

                container.Controls.Add(new HyperLink() { NavigateUrl = bookmarkletHref, Text = "Post Message" });

                Controls.Add(container);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }
    }
}
